package com.js.common.core;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.js.domian.model.bot.analysis.Message;
import com.js.domian.model.bot.analysis.message.*;
import org.springframework.util.StringUtils;

public class MessageTypeHandle {

    public static Message getMessage(JSONObject jsonObject) {
        if (jsonObject == null) {
            return null;
        }
        String type = jsonObject.getStr("type");
        if (StringUtils.isEmpty(type)) {
            return null;
        }
        Class<? extends Message> messageClass;
        switch (type) {
            case "text":
                messageClass = TextMessage.class;
                break;
            case "face":
                messageClass = FaceMessage.class;
                break;
            case "record":
                messageClass = RecordMessage.class;
                break;
            case "image":
                messageClass = ImageMessage.class;
                break;
            case "at":
                messageClass = AtMessage.class;
                break;
            case "video":
                messageClass = VideoMessage.class;
                break;
            case "share":
                messageClass = ShareMessage.class;
                break;
            case "reply":
                messageClass = ReplyMessage.class;
                break;
            case "json":
                messageClass = JsonMessage.class;
                break;
            default:
                return new UnknownMessage() {{
                    setJson(JSONUtil.toJsonStr(jsonObject));
                }};
        }
        return jsonObject.get("data", messageClass);
    }

}
